<?php
namespace app\home\model;

//加载配置类
use think\Config;
//导入Controller
use think\Model;
//导入Db
use think\Db;
//引入Session类
use think\Session;

class UserModel extends Model
{
	/**
	*	获取该登录用户的个人信息
	*/
	public function getUser()
	{
		$uid = Session::get('user_id');
		$result = Db::table('sz_user')->where('id',$uid)->find();
		return $result;
	}


	/**
	*	获取该登录用户默认的收货地址
	*/
	public function getDefaultAddress()
	{
		$uid = Session::get('user_id');
		$where = [];
		$where['uid'] = $uid;
		$where['a_status'] = 1;
		$result = Db::table('sz_address')->where($where)->find();

		return $result;
	}

	/**
	*	修改联系方式
	*/
	public function editphone($data)
	{
		$uid = Session::get('user_id');
		$result = Db::table('sz_user')->where('id',$uid)->update(['u_phone'=>$data['u_phone']]);
		return true;
	}

	/**
	*	修改会员名字
	*/
	public function editusername($data)
	{
		$uid = Session::get('user_id');
		$result = Db::table('sz_user')->where('id',$uid)->update(['u_name'=>$data['u_name']]);
		return true;
	}

	/**
	*	修改会员邀请ID
	*/
	public function edituserrecomid($data)
	{
		$uid = Session::get('user_id');
		$result = Db::table('sz_user')->where('id',$uid)->update(['recom_id'=>$data['recom_id']]);
		return true;
	}

	/**
	*	修改店铺名称
	*/
	public function edituserstore($data)
	{
		$uid = Session::get('user_id');
		$result = Db::table('sz_user')->where('id',$uid)->update(['u_store'=>$data['u_store']]);
		return true;
	}

	/**
	*	收货地址列表
	*/
	public function getUserAddressIndex()
	{
		$uid = Session::get('user_id');
		$a_status = 2;
		$result = Db::table('sz_address')->where(['uid'=>$uid])->select();
		return $result;
	}

	/**
	*	添加会员的收货地址
	*/
	public function addUserAddress($data)
	{
		$uid = Session::get('user_id');	
		$data['uid'] = $uid;
		$data['addtime'] = strtotime(date('Y-m-d H:i',time()));
		$result = Db::table('sz_address')->insert($data);
		return true;
	}

	/**
	*	修改会员收货地址
	*/
	public function editAddress($id)
	{
		$result = Db::table('sz_address')->where('id',$id)->find();
		return $result;
	}

	/**
	*	修改会员收货地址
	*/
	public function editAddressPost($id,$data)
	{
		if($data){
			$data['addtime'] = strtotime(date('Y-m-d H:i',time()));
			$uid = Session::get('user_id');
			if($data['a_status']==1){
				$res = Db::table('sz_address')->where(['uid'=>$uid])->update(['a_status'=>2]);
				$result = Db::table('sz_address')->where('id',$id)->update($data);
				return true;

			}else{

				$result = Db::table('sz_address')->where('id',$id)->update($data);
				return true;
			}
			
		}else{
			return false;
		}
		
	}

	/**
	*	选择哪个为默认地址
	*/
	public function editDaddressPost($id)
	{
		if($id){
			$data['addtime'] = strtotime(date('Y-m-d H:i',time()));
			$data['a_status'] = 1;
			$uid = Session::get('user_id');
			$res = Db::table('sz_address')->where(['uid'=>$uid])->update(['a_status'=>2]);
			$result = Db::table('sz_address')->where('id',$id)->update($data);
			return true;
		}else{
			return false;
		}
	}

	/**
	*	删除默认地址
	*/
	public function deleteUserAddress($id)
	{
		if($id){
			$res = Db::table('sz_address')->where('id',$id)->find();
			if($res['a_status'] === 1){
				return 3;
			}else{
				$result = Db::table('sz_address')->where('id',$id)->delete();
				return 2;
			}
			
		}else{
			return 4;
		}
	}

	/**
	*	获取官方活动
	*/
	public function getevents()
	{
		$where['g_show'] = 2;
		$where['g_status'] = 2;
		$result = Db::table('sz_goods')->where($where)->find();

		return $result;
	}

	/**
	*	订单列表
	*  @param $o_status  订单状态
	*/
	public function getUserOrder($o_status)
	{
		$uid = Session::get('user_id');
		$res = Db::table('sz_orders')->where(['uid'=>$uid,'o_status'=>$o_status])->select();
		$arr = [];
		$list = [];
		foreach($res as $key=>$val){
			$arr[$key] = Db::table('sz_goods')->where('id','in',$val['gids'])->select();
			$list[$key]['id'] = $val['id'];
			$list[$key]['uid'] = $val['uid'];
			$list[$key]['aid'] = $val['aid'];
			$list[$key]['gids'] = $val['gids'];
			$list[$key]['o_code'] = $val['o_code'];
			$list[$key]['o_tprice'] = $val['o_tprice'];
			$list[$key]['o_status'] = $val['o_status'];
			$list[$key]['o_time'] = $val['o_time'];
			$list[$key]['o_type'] = $val['o_type'];
			$list[$key]['o_message'] = $val['o_message'];
			$list[$key]['o_company'] = $val['o_company'];
			$list[$key]['o_snumber'] = $val['o_snumber'];
			$list[$key]['addtime'] = $val['addtime'];
			foreach($arr[$key] as $k=>$v){
				$list[$key]['goods'][$k]['id'] = $v['id'];
				$list[$key]['goods'][$k]['cid'] = $v['cid'];
				$list[$key]['goods'][$k]['g_type'] = $v['g_type'];
				$list[$key]['goods'][$k]['g_name'] = $v['g_name'];
				$list[$key]['goods'][$k]['g_cprice'] = $v['g_cprice'];
				$list[$key]['goods'][$k]['g_oprice'] = $v['g_oprice'];
				$list[$key]['goods'][$k]['g_pic'] = json_decode($v['g_pic'],true);
				$list[$key]['goods'][$k]['g_description'] = $v['g_description'];
				$list[$key]['goods'][$k]['g_content'] = $v['g_content'];
				$list[$key]['goods'][$k]['g_status'] = $v['g_status'];
				$list[$key]['goods'][$k]['g_show'] = $v['g_show'];
				$list[$key]['goods'][$k]['g_tnumber'] = $v['g_tnumber'];
				$list[$key]['goods'][$k]['g_unit'] = $v['g_unit'];
				$list[$key]['goods'][$k]['g_number'] = $v['g_number'];
				$list[$key]['goods'][$k]['starttime'] = $v['starttime'];
				$list[$key]['goods'][$k]['endtime'] = $v['endtime'];
				$list[$key]['goods'][$k]['addtime'] = $v['addtime'];
			}
		}
		return $list;
	}

	/**
	*	订单列表-商品信息
	* @param $gids  商品的id集  
	*/
	public function getOrderGoods($gids)
	{
		if($gids){
			$res = Db::table('sz_goods')->where('id','in',$gids)->select();
			return $res;
		}
	}

	/**
	*	订单列表-商品规格信息
	*	@param  $gid  商品id
	*	@param  $oid  订单id
	*/
	public function getOrderGoodsInfo($gid,$oid)
	{
		$res = Db::table('sz_orderinfo')->alias('oi')
			 ->join('sz_info i','oi.iid=i.id','left')
			 ->field('oi.*,i.i_name,i.i_price,i.i_number,i.i_commission')
			 ->where('oi.gid',$gid)
			 ->where('oi.oid',$oid)
			 ->select();
		return $res;
	}

	/**
	*	我的支出
	*/
	public function getUserSpend()
	{
		$uid = Session::get('user_id');
		$res = Db::table('sz_spending')->alias('s')
			 ->join('sz_orders o','s.oid=o.id','left')
			 ->field('s.*,o.o_code,o.o_time,o.o_tprice')
			 ->where('s.uid',$uid)
			 ->select();
		return $res;
	}

	/**
	*	订单物流信息
	*   $id  订单id
	*/
	public function getOrderLog($id)
	{
		if($id){
			$res = Db::table('sz_orders')->where('id',$id)->find();
			return $res;
		}else{
			return false;
		}
	}

	/**
	*	专柜列表
	*/
	public function getApply()
	{
		$res = Db::table('sz_counters')->select();
		return $res;
	}


	/**
	*	合伙人列表
	*/
	public function getPartner()
	{
		$res = Db::table('sz_partnership')->select();
		return $res;
	}

	/**
	*	商品佣金列表
	*/
	public function getCommission()
	{
		$res = Db::table('sz_goods')->where('g_status',2)->select();

		foreach($res as $key=>$val){
			$res[$key]['goodsinfo'] = Db::table('sz_info')->where('gid',$val['id'])->select();
		}
		// dump($res);exit;
		return $res;
	}

	/* 用户提现 */
	public function addWithDrawal($data)
	{
		if($data){
			$data['uid'] = Session::get('user_id');
			$data['addtime'] = strtotime(date('Y-m-d H:i',time()));
			$res = Db::table('sz_withdrawal')->insertGetId($data);
			$result = Db::table('sz_user')->where('id',$data['uid'])->find();
			$u_wallet = $result['u_wallet'] - $data['w_amount'];
			Db::table('sz_user')->where('id',$data['uid'])->update(['u_wallet'=>$u_wallet]);

			return $res;
			
		}else{
			return false;
		}
	}

	/**
	*	用户提现提交
	*  $id  提现id
	*/
	public function getOneWithDrawal($id)
	{
		if($id){
			$res = Db::table('sz_withdrawal')->alias('wd')
				 ->join('sz_user u','wd.uid = u.id','left')
				 ->field('wd.*,u.u_wallet')
				 ->where('wd.id',$id)
				 ->find();
			return $res;
		}else{
			return false;
		}
	}

	/**
	*	用户提现记录
	*/

	public function getWithDrawalIndex()
	{
		$uid = Session::get('user_id');
		$res = Db::table('sz_withdrawal')->where('uid',$uid)->select();
		return $res;
	}
}